using System;

namespace Azure.Domain.Processors
{
    public abstract class BaseProcessor : IProcessor
    {
        private ProcessorState _state;
        private bool _isDisposed;

        protected BaseProcessor()
        {
            _state = ProcessorState.Initialized;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                _state = ProcessorState.Uninitialized;
                _isDisposed = true;
            }
        }

        public void Dispose()
        {
            this.Dispose(true);
        }

        public void Start()
        {
            EnsureNotDisposed();
            OnStart();
            _state = ProcessorState.Running;
        }

        public void Stop()
        {
            EnsureNotDisposed();
            OnStop();
            _state = ProcessorState.Stopped;
        }

        protected abstract void OnStart();

        protected abstract void OnStop();

        protected void EnsureNotDisposed()
        {
            if (_isDisposed)
            {
                throw new InvalidOperationException("Cannot be performed while class has been disposed.");
            }
        }

        public ProcessorState State
        {
            get
            {
                return _state;
            }
        }
    }
}